<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:r="http://www.w3.org/1999/XSL/Transform_RESULT"
  xmlns:e="livcos.org/ns/editor"
  xmlns:t="livcos.org/ns/cosmos/type"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="job e t">
  
  <xsl:output method="xml" encoding="UTF-8"/>
  
  <xsl:param name="objId"/>
  <xsl:param name="scope" select="'content'"/>
	<xsl:param name="id"/>
	<xsl:param name="editorId"/>
	<xsl:param name="type"/>
	<xsl:param name="parentId"/>
	<xsl:param name="tmplObjId"/>
	<xsl:param name="tmplId"/>
	<xsl:param name="tmplArg1"/>
	<xsl:param name="tmplArg2"/>
	<xsl:param name="tmplArg3"/>
	<xsl:param name="tmplArg4"/>
	<xsl:param name="tmplArg5"/>
	
	<xsl:param name="_editorBase" select="'/livcos.org/type/editor/'"/>
	<xsl:param name="_contentExt" select="'.content'"/>
	
  <xsl:namespace-alias stylesheet-prefix="r" result-prefix="xsl"/>

  <xsl:template match="/">
    <xsl:apply-templates select="document(concat($_editorBase, $type, $_contentExt))/*"/>
	</xsl:template>
	
	<xsl:template match="e:editor">
		<job:transform>
			<r:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
				<xsl:variable name="ns_nodes"
					select="namespace::*[(local-name(.) != 'xml') and (local-name(.) != '')]"/>
				<xsl:for-each-group group-by="." select="$ns_nodes">
					<xsl:copy-of select="."/>
				</xsl:for-each-group>
				<xsl:attribute name="exclude-result-prefixes">
					<xsl:for-each-group group-by="." select="$ns_nodes">
						<xsl:value-of select="local-name(.)"/>
						<xsl:text> </xsl:text>
					</xsl:for-each-group>
				</xsl:attribute>

				<r:import href="/livcos.org/web/editor/EntityEditorContent.xsl"/>

				<r:variable name="objId" select="'{$objId}'"/>
				<r:variable name="scope" select="'{$scope}'"/>
				<r:variable name="id" select="'{$id}'"/>
				
				<xsl:if test="$tmplObjId">
					<r:variable name="data" as="node()?">
						<xsl:variable name="_tmplArgs">
							<xsl:text>arg1=</xsl:text>
							<xsl:value-of select="encode-for-uri($tmplArg1)"/>
							<xsl:text>&amp;arg2=</xsl:text>
							<xsl:value-of select="encode-for-uri($tmplArg2)"/>
							<xsl:text>&amp;arg3=</xsl:text>
							<xsl:value-of select="encode-for-uri($tmplArg3)"/>
							<xsl:text>&amp;arg4=</xsl:text>
							<xsl:value-of select="encode-for-uri($tmplArg4)"/>
							<xsl:text>&amp;arg5=</xsl:text>
							<xsl:value-of select="encode-for-uri($tmplArg5)"/>
						</xsl:variable>
						<xsl:variable name="_tmpl"
							select="doc(concat($tmplObjId, '.view?', $_tmplArgs))/*"/>
						<xsl:choose>
							<xsl:when test="$tmplId">
								<xsl:copy-of select="$_tmpl/*[@id = $tmplId]/t:content/node()"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:copy-of select="$_tmpl/*[1]/t:content/node()"/>
							</xsl:otherwise>
						</xsl:choose>
					</r:variable>
				</xsl:if>

				<r:template name="printBody">
					<xsl:variable name="editors" select="e:input | e:editor"/>
					<xsl:choose>
						<xsl:when test="normalize-space($editorId) != ''">
							<xsl:apply-templates select="$editors[@id = $editorId]/*"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:apply-templates select="$editors[1]/*"/>
						</xsl:otherwise>
					</xsl:choose>
				</r:template>
				
				<r:template name="printNamespaceDecls">
					<xsl:for-each-group select="$ns_nodes" group-by=".">
						<input name="ns-{local-name(.)}" value="{.}" type="hidden"/>
					</xsl:for-each-group>
					<xsl:if test="namespace-uri-for-prefix('', .) = ''">
						<input name="ns" value="" type="hidden"/>
					</xsl:if>
				</r:template>

				<r:template name="printParameters">
					<xsl:if test="$tmplObjId">
						<input type="hidden" name="id" value="{$parentId}"/>
						<input type="hidden" name="tmplObjId" value="{$tmplObjId}"/>
						<input type="hidden" name="tmplId" value="{$tmplId}"/>
					</xsl:if>
				</r:template>
				
			</r:stylesheet>
		</job:transform>
	</xsl:template>
  
  <xsl:template match="e:info">
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <r:value-of select="{@path}"/>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:text | e:integer">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
			  <!-- overflow:auto div to work around input cursor bug in Firefox -->
			  <div>
			    <input name="content_{$inputCount}" value="{{{@path}}}" type="text"
			      onfocus="this.parentNode.style.overflow = 'auto'"
			      onblur="this.parentNode.style.overflow = 'visible'">
			    	<xsl:if test="@onChange">
							<xsl:attribute name="onkeyup">
								<xsl:text>this.form._panel.ed_</xsl:text>
								<xsl:value-of select="@onChange"/>
								<xsl:text>(this, event, '</xsl:text>
								<xsl:value-of select="@path"/>
								<xsl:text>')</xsl:text>
							</xsl:attribute>
			    	</xsl:if>
			    	<xsl:if test="not((@path = '.') or contains(@path,'title') or contains(@path,'label'))">
			    		<xsl:attribute name="autocomplete">off</xsl:attribute>
			    	</xsl:if>
			    	<xsl:if test="local-name() = 'integer'">
			    		<xsl:attribute name="style">width: 3em; text-align: right;</xsl:attribute>
			    	</xsl:if>
			    </input>
			  	<xsl:if test="@unit">
			  		<span style="font-size: 80%"><xsl:value-of select="@unit"/></span>
			  	</xsl:if>
			  </div>
      </td>
    </tr>
  </xsl:template>
	
	<xsl:template match="e:text[@multiline = '1']">
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
		  <xsl:if test="@label">
  			<td class="ed_label">
  				<xsl:value-of select="@label"/>
  				<xsl:text>:</xsl:text>
  			</td>
		  </xsl:if>
			<td class="ed_entry">
        <xsl:if test="not(@label)">
          <xsl:attribute name="colspan">2</xsl:attribute>
        </xsl:if>
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
			  <xsl:choose>
			    <xsl:when test="@plain = 1">
			      <input name="type_{$inputCount}" value="plain" type="hidden"/>
			      <div style="overflow: auto;">
			        <textarea class="input" name="content_{$inputCount}">
			          <r:apply-templates select="{@path}" mode="root_escape"/>
			        </textarea>
			      </div>
			    </xsl:when>
			    <xsl:otherwise>
			      <div class="richtext_input" name="content_{$inputCount}">
			        <r:apply-templates select="{@path}" mode="root_content"/>
			      </div>
			    </xsl:otherwise>
			  </xsl:choose>
			</td>
		</tr>
	</xsl:template>
	
  <xsl:template match="e:choice">
		<!-- TODO 9: exclusive switch... -->
		<xsl:apply-templates select="e:option/*"/>
	</xsl:template>
	
  <xsl:template match="e:ref | e:url | e:user">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
      	<xsl:choose>
      		<xsl:when test="@text">
      			<r:variable name="val" select="{@text}"/>
      		</xsl:when>
      		<xsl:otherwise>
      			<r:variable name="val" select="{@path}"/>
      		</xsl:otherwise>
      	</xsl:choose>
      	<div class="ref_input">
          <input name="content_{$inputCount}" value="{{$val}}" type="text" autocomplete="off"/>
        </div>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:time">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <table cellspacing="0" cellpadding="0">
          <tr>
            <td>
              <!-- overflow:auto div to work around input cursor bug in Firefox -->
              <div>
                <input name="content_{$inputCount}" value="{{{@path}}}" type="text"
                  onfocus="this.parentNode.style.overflow = 'auto'" onblur="this.parentNode.style.overflow = 'visible'"
                />
              </div>
            </td>
            <td>
              <input type="button" value="now">
                <xsl:attribute name="onclick">
                  <xsl:text>this.form.content_</xsl:text>
                  <xsl:value-of select="$inputCount"/>
                  <xsl:text>.value = (new Date()).format('HH:mm:ss');</xsl:text>
                </xsl:attribute>
              </input>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:date | e:dateTime">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <div class="{local-name()}_input">
          <input name="content_{$inputCount}" value="{{{@path}}}" type="text"/>
        </div>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:percentage">
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<select name="content_{$inputCount}">
				  <option value="">-</option>
				  <xsl:variable name="currentValue" select="@path"/>
					<xsl:for-each select="0 to 10">
						<xsl:variable name="val" select=". * 10"/>
						<option value="{$val}">
						  <r:if test="({$currentValue} != '') and ({$currentValue} = {$val})">
								<r:attribute name="selected">true</r:attribute>
							</r:if>
							<xsl:value-of select="$val"/>
						  <xsl:text>%</xsl:text>
						</option>
					</xsl:for-each>
				</select>
			</td>
		</tr>
	</xsl:template>
	
  <xsl:template match="e:priority">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <select name="content_{$inputCount}">
          <xsl:variable name="currentValue" select="@path"/>
          <xsl:for-each select="1 to 5">
            <option value="{.}">
							<r:if
								test="(({$currentValue} != '') and ({$currentValue} = {.})) or (({$currentValue} = '') and ({.} = 3))">
								<r:attribute name="selected">true</r:attribute>
							</r:if>
              <xsl:value-of select="."/>
              <xsl:choose>
              	<xsl:when test=". = 1"> (critical)</xsl:when>
              	<xsl:when test=". = 2"> (high)</xsl:when>
              	<xsl:when test=". = 3"> (normal)</xsl:when>
              	<xsl:when test=". = 4"> (low)</xsl:when>
              	<xsl:when test=". = 5"> (none)</xsl:when>
              </xsl:choose>
            </option>
          </xsl:for-each>
        </select>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:enum">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <select name="content_{$inputCount}">
          <xsl:variable name="currentValue" select="@path"/>
          <xsl:for-each select="e:option">
            <option value="{@value}">
              <r:if test="({$currentValue} != '') and ({$currentValue} = '{@value}')">
                <r:attribute name="selected">true</r:attribute>
              </r:if>
              <xsl:value-of select="."/>
            </option>
          </xsl:for-each>
        </select>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="e:boolean">
    <xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
    <tr>
      <td class="ed_label">
        <xsl:value-of select="@label"/>
        <xsl:text>:</xsl:text>
      </td>
      <td class="ed_entry">
        <input name="path_{$inputCount}" value="{@path}" type="hidden"/>
        <input id="content_{$inputCount}" name="content_{$inputCount}" value="{{{@path}}}" type="hidden"/>
        <input type="checkbox" style="width: auto"
          onclick="document.getElementById('content_{$inputCount}').value = (this.checked)?'1':'0'">
          <r:if test="{@path} = '1'">
            <r:attribute name="checked">true</r:attribute>
          </r:if>
        </input>
      </td>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>
